package com.fqdf.framework.android.service.downloadservice;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author 张枭
 * @version V1.0
 * @date 2017/3/9
 * @desc
 */
public class DownloadBean implements Parcelable {
    // 下载后文件存储位置
    private String savePath;
    // 下载文件的url地址
    private String url;
    // 如果存在相同的文件是否进行跳过
    private boolean isSkip;

    private DownloadConstans.RESULT result;

    public String getSavePath() {
        return savePath;
    }

    public String getUrl() {
        return url;
    }

    public boolean isSkip() {
        return isSkip;
    }

    public DownloadConstans.RESULT getResult() {
        return result;
    }

    public void setResult(DownloadConstans.RESULT result) {
        this.result = result;
    }

    private DownloadBean(Builder builder) {
        savePath = builder.savePath;
        url = builder.url;
        isSkip = builder.isSkip;
        result = builder.result;
    }


    public static final class Builder {
        private String savePath;
        private String url;
        private boolean isSkip;
        private DownloadConstans.RESULT result;

        public Builder() {
        }

        public Builder withSavePath(String val) {
            savePath = val;
            return this;
        }

        public Builder withUrl(String val) {
            url = val;
            return this;
        }

        public Builder withIsSkip(boolean val) {
            isSkip = val;
            return this;
        }

        public Builder withResult(DownloadConstans.RESULT val) {
            result = val;
            return this;
        }

        public DownloadBean build() {
            return new DownloadBean(this);
        }
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.savePath);
        dest.writeString(this.url);
        dest.writeByte(this.isSkip ? (byte) 1 : (byte) 0);
        dest.writeInt(this.result == null ? -1 : this.result.ordinal());
    }

    protected DownloadBean(Parcel in) {
        this.savePath = in.readString();
        this.url = in.readString();
        this.isSkip = in.readByte() != 0;
        int tmpResult = in.readInt();
        this.result = tmpResult == -1 ? null : DownloadConstans.RESULT.values()[tmpResult];
    }

    public static final Creator<DownloadBean> CREATOR = new Creator<DownloadBean>() {
        @Override
        public DownloadBean createFromParcel(Parcel source) {
            return new DownloadBean(source);
        }

        @Override
        public DownloadBean[] newArray(int size) {
            return new DownloadBean[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DownloadBean)) return false;

        DownloadBean that = (DownloadBean) o;

        if (isSkip() != that.isSkip()) return false;
        if (!getSavePath().equals(that.getSavePath())) return false;
        if (!getUrl().equals(that.getUrl())) return false;
        return getResult() == that.getResult();

    }

    @Override
    public int hashCode() {
        int result1 = getSavePath().hashCode();
        result1 = 31 * result1 + getUrl().hashCode();
        result1 = 31 * result1 + (isSkip() ? 1 : 0);
        result1 = 31 * result1 + getResult().hashCode();
        return result1;
    }

    @Override
    public String toString() {
        return "DownloadBean{" +
                "savePath='" + savePath + '\'' +
                ", url='" + url + '\'' +
                ", isSkip=" + isSkip +
                ", result=" + result +
                '}';
    }
}
